﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using JulMar.Silverlight.Interfaces;
using JulMar.Silverlight.Mvvm;
using MediaBrowserSL.RIA.Models;
using MediaBrowserSL.RIA.MvvmUtils;
using MediaBrowserSL.RIA.Services;
using System.ServiceModel;
using MediaBrowserSL.RIA.MediaBrowserServiceReference;
using System.Windows;
using MediaBrowserSL.RIA.MvvmUtils.UI;
using Jade.Tools.SL.Windows.Extensions;

namespace MediaBrowserSL.RIA.ViewModels
{
    /// <summary>
    /// Settings View Model
    /// </summary>
    public class SettingsViewModel : ViewModelBase
    {
        #region Private Member

        private IMediaRiaServices service;
        private IErrorVisualizer errorVisualizer;
        private ObservableCollection<ServerSpace> serverSpaces;
        private ICustomMessageBoxVisualizer customMessageBoxVisualizer;
        private bool isGetting;

        #endregion

        #region Public Member

        public ObservableCollection<ServerSpace> ServerSpaces
        {
            get
            {
                return serverSpaces;
            }
            set
            {
                serverSpaces = value;
                OnPropertyChanged("ServerSpaces");
            }
        }

        public bool IsGetting
        {
            get { return isGetting; }
            set
            {
                isGetting = value;
                OnPropertyChanged("IsGetting");
            }
        }

        #endregion

        #region Ctor

        public SettingsViewModel(IMediaRiaServices service)
        {
            this.service = service;
            errorVisualizer = Resolve<IErrorVisualizer>();
            customMessageBoxVisualizer = Resolve<ICustomMessageBoxVisualizer>();
            RegisterWithMessageMediator();
        }

        #endregion

        #region Private Method

        private void OnGetSize(Dictionary<int, double> lSize, Exception ex)
        {
            IsGetting = false;
            if (ex == null)
            {
                ObservableCollection<ServerSpace> lTmp = new ObservableCollection<ServerSpace>();
                foreach (KeyValuePair<int, double> s in lSize)
                {
                    if (s.Key == -1)
                    {
                        lTmp.Add(new ServerSpace { PartName = "Espace libre", Size = s.Value });
                    }
                    else if (s.Key == 1)
                    {
                        lTmp.Add(new ServerSpace { PartName = "Espace Films", Size = s.Value });
                    }
                    else if (s.Key == 2)
                    {
                        lTmp.Add(new ServerSpace { PartName = "Espace Musiques", Size = s.Value });
                    }
                    else if (s.Key == 3)
                    {
                        lTmp.Add(new ServerSpace { PartName = "Espace Images", Size = s.Value });
                    }
                    else if (s.Key == 4)
                    {
                        lTmp.Add(new ServerSpace { PartName = "Espace Livres", Size = s.Value });
                    }
                }
                ServerSpaces = lTmp;                
            }
            else
            {
                if (customMessageBoxVisualizer != null)
                {
                    customMessageBoxVisualizer.Show(null, ((FaultException<ErrorDataContract>)ex).Detail.Detail, ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        #endregion

        #region Public Method

        /// <summary>
        /// Show Settings View
        /// </summary>
        /// <param name="sender"></param>
        [MessageMediatorTarget(MediatorMessages.ShowSettingsView)]
        public void ShowSettingsView(object sender)
        {
            if (!isGetting)
            {
                service.GetSize(OnGetSize, SessionMb.CurrentUser);
                IsGetting = true;
            }
        }

        /// <summary>
        /// Show Settings View
        /// </summary>
        /// <param name="sender"></param>
        [MessageMediatorTarget(MediatorMessages.CloseSettingsView)]
        public void CloseSettingsView(object sender)
        {
            if (ServerSpaces != null) { ServerSpaces.Clear(); }
        }

        #endregion
    }
}
